package com.smec.moblie.util.imageuploader;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.smec.moblie.util.ConfigContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by snipking on 14-2-26.
 */
public class UploadList {

    private static UploadList uploadList;
    private static Context context;
    private SharedPreferences storedUploadList;
    private static Gson gson;

    private UploadList() {
        gson = new Gson();
    }

    private UploadList(Context context) {
        this.context = context;
        storedUploadList = context.getSharedPreferences(ConfigContext.UPLOAD_LIST_SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
        gson = new Gson();
    }

    public static UploadList getInstance(Context context) {
        synchronized(UploadList.class) {
            if(context != null) {
                if(context != UploadList.context) {
                    uploadList = new UploadList(context);
                }
            } else {
                // TODO replace with custom exception
                throw new RuntimeException("context is null");
            }
        }
        return uploadList;
    }

    public static UploadList getInstanceNew(Context context) {
    	UploadList.context = context;
        synchronized(UploadList.class) {
            if(context != null) {
                if(context != UploadList.context) {
                    uploadList = new UploadList();
                }
            } else {
                // TODO replace with custom exception
                throw new RuntimeException("context is null");
            }
        }
        return uploadList;
    }

    public void addToUploadList(FndResource resource) {
        SharedPreferences.Editor editor = storedUploadList.edit();
        editor.putString(resource.getResourceId(), gson.toJson(resource));
        editor.commit();
    }

    public void removeFromUploadList(String resourceId) {
        SharedPreferences.Editor editor = storedUploadList.edit();
        editor.remove(resourceId);
        editor.commit();
    }

    public void bulkAddToUploadList(List<FndResource> resources) {
        if(resources != null)
        {
            SharedPreferences.Editor editor = storedUploadList.edit();
            for(FndResource resource : resources) {
                editor.putString(resource.getResourceId(), gson.toJson(resource));
            }
            editor.commit();
        }
    }

    public List<FndResource> getToUploadList() {
        Map resourceMap = storedUploadList.getAll();
        Set resourceIds = resourceMap.keySet();
        ArrayList resourceList = new ArrayList<FndResource>();
        for (Object resourceId : resourceIds) {
            resourceList.add(gson.fromJson((String)resourceMap.get(resourceId), FndResource.class));
        }
        return resourceList;
    }

    public Boolean isResourceInUploadList(String resourceId) {
    		String jsonObj = (String)storedUploadList.getAll().get(resourceId);
    		if(jsonObj != null) {
    			return true;
    		}
    		return false;
    }

    public Map<String, Boolean> isResourceInUploadList(List<String> resourceIds) {
    		Map<String, Boolean> result = new HashMap<String, Boolean>();
    		for(String resourceId : resourceIds) {
    			result.put(resourceId, isResourceInUploadList(resourceId));
    		}
//    		return null;
    		return result;
    }
}
